package com.aaa.tmr.service.impl;

import com.aaa.tmr.entity.Custom;
import com.aaa.tmr.entity.User;
import com.aaa.tmr.entity.Visit;
import com.aaa.tmr.dao.VisitDao;
import com.aaa.tmr.listener.DataListener;
import com.aaa.tmr.service.VisitService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * (Visit)表服务实现类
 *
 * @author makejava
 * @since 2021-03-23 15:14:19
 */
@Service("visitService")
public class VisitServiceImpl implements VisitService {
    @Resource
    private VisitDao visitDao;

    @Resource
    private HttpSession session;

    @Resource
    private HttpServletResponse response;

    /**
     * 通过ID查询单条数据
     *
     * @param visitId 主键
     * @return 实例对象
     */
    @Override
    public Visit queryById(Integer visitId) {
        return this.visitDao.queryById(visitId);
    }

    /**
     * 通过实体作为筛选条件分页查询
     *
     * @param visit 实例对象
     * @return 对象列表
     */
    @Override
    public PageInfo queryAll(int pageNo, int pageSize, Visit visit) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<Visit>(this.visitDao
                .queryAll(visit));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Visit> queryAllByLimit(int offset, int limit) {
        return this.visitDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param visit 实例对象
     * @return 实例对象
     */
    @Override
    public Visit insert(Visit visit) {
        User user = (User) session.getAttribute("userInfo");
        if (user != null) {
            visit.setCreateBy(user.getUserName());
            visit.setCreateTime(new Date());
            this.visitDao.insert(visit);
        }
        return visit;
    }

    /**
     * 修改数据
     *
     * @param visit 实例对象
     * @return 实例对象
     */
    @Override
    public Visit update(Visit visit) {
        User user = (User) session.getAttribute("userInfo");
        if (user != null) {
            visit.setUpdateBy(user.getUserName());
            visit.setUpdateTime(new Date());
            this.visitDao.update(visit);
        }
        return this.queryById(visit.getVisitId());
    }

    /**
     * 通过主键删除数据
     *
     * @param visitId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer visitId) {
        return this.visitDao.deleteById(visitId) > 0;
    }

    /**
     * 通过主键批量删除数据
     *
     * @param visitIds 主键
     * @return 影响行数
     */
    @Override
    public int deleteByIds(String visitIds) {
        return this.visitDao.deleteByIds(visitIds);
    }

    @Override
    public boolean importVisit(MultipartFile file) {
        ExcelReader excelReader = null;
        try {
            DataListener dataListener = new DataListener();
            excelReader = EasyExcel.read(file.getInputStream(), Visit.class, dataListener).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
            Boolean isSuc = true;
            List list = dataListener.getList();
            if (!CollectionUtils.isEmpty(list)) {
                for (Object o : list) {
                    Visit visit = (Visit) o;
                    Visit visitInsert = this.insert(visit);
                    if (visitInsert == null) {
                        isSuc = false;
                    }
                }
            }
            return isSuc;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (excelReader != null) {
                excelReader.finish();
            }
        }
        return false;
    }

    @Override
    public boolean exportVisit(Visit visit) {
        ExcelWriter excelWriter = null;
        try {

            OutputStream outputStream = response.getOutputStream();
            //设值导出内容的类型及编码方式
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设值头部信息，主要设置导出文件名称
            response.setHeader("Content-Disposition","attachment;filename="+System.currentTimeMillis()+".xlsx");
            List<Visit> visitList = visitDao.queryAll(visit);
            excelWriter = EasyExcel.write(outputStream, Visit.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("回访信息").build();
            excelWriter.write(visitList, writeSheet);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
        } finally{
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
        return false;
    }
}
